package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class StatementServiceImpl implements IStatementService {

    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementItemMapper statementItemMapper ;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Statement>(statementMapper.selectForList(qo));
    }


    @Override
    public void save(Statement statement) {
        statement.setCreateTime(new Date());
        statementMapper.insert(statement);
    }

    @Override
    public Statement get(Long id) {
        return statementMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Statement statement) {
        if(!Statement.STATUS_CONSUME.equals(statement.getStatus())){
            throw new BusinessException("只有在消费状态才能编辑结算单");
        }
        statementMapper.updateByPrimaryKey(statement);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Statement> list() {
        return statementMapper.selectAll();
    }

    @Override
    public void payStatement(Long statementId) {
        User user = ShiroUtils.getUser();
        statementMapper.payStatement(statementId,user.getId(),new Date(),Statement.STATUS_PAID);
    }

    @Override
    public Statement queryStatementByAppointmentId(Long id) {

        return statementMapper.selectStatementByAppointmentId(id);
    }

    @Override
    public Statement saveStatement(Long id) {
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        appointmentMapper.changeStatus(id,Appointment.STATUS_SETTLE);
        Statement statement = this.generateStatement(appointment);
        statementMapper.insert(statement);
        return statement ;


    }

    @Override
    public void delete(Long id) {
        statementMapper.deleteByPrimaryKey(id);
        statementItemMapper.deleteByStatementId(id);

    }

    public Statement generateStatement(Appointment appointment) {
        Statement statement = new Statement();
        statement.setCreateTime(new Date());
        statement.setActualArrivalTime(appointment.getActualArrivalTime());//实际到店时间
        statement.setCarSeries(appointment.getCarSeries());//汽车类型
        statement.setCustomerName(appointment.getCustomerName());//客户名称
        statement.setLicensePlate(appointment.getLicensePlate());//车牌号码
        statement.setAppointmentId(appointment.getId());//预约单ID
        statement.setServiceType(appointment.getServiceType() + 0L);//服务类型
        statement.setCustomerPhone(appointment.getCustomerPhone());//用户联系方式
        return statement ;

    }



}
